(function() {
    'use strict';

    /**
     * @ngdoc function
     * @name webApp.controller:CouponCtrl
     * @description
     * # CouponCtrl
     * Controller of the webApp
     */
    angular.module('webApp')
        .controller('CouponCtrl', CouponCtrl);

    CouponCtrl.$inject = ['$scope', 'dataService', '$location', 'helper', '$rootScope', '$timeout', 'sessionService'];

    function CouponCtrl($scope, dataService, $location, helper, $rootScope, $timeout, sessionService) {

        $scope.listAction = function() {
            $scope.pageSize = 10;

            $scope.$watch('storeary', function(newValue, oldValue, scope) {
                if (newValue) {
                    $scope.principal_name = newValue.principal_name;
                    $scope.storeid = newValue.store_id;
                    $scope.getList(1);
                }
            }, true);
        }

        //卡券ID列表
        $scope.getList = function(pageNo) {
            var tokenUser = sessionService.get('tokenUser');
            var partner_id = tokenUser.CompanyId;

            var data = {
                "method": "dhf.card.list.get",
                "content": { "gzh_appid": $scope.wxappid, "page_size": $scope.pageSize, "page_no": pageNo }
            };

//             console.error("data-->", data);
            // return false;
            dataService.cardListGet(data).then(function(result) {
//                 console.error("卡券列表--->", result, result.errmsg);

                if (result.total_count) {
                    $scope.card_list = result.card_list;
                    $scope.total_count = result.total_count;
                    $scope.wxappid = $scope.wxappid;
                } else {
                    helper.myAlert("error: 请先下载卡券数据！");
                }
            });
        }

        //下载卡券数据
        $scope.downloadCoupon = function() {
            var tokenUser = sessionService.get('tokenUser');
            var partner_id = tokenUser.CompanyId;

            var data = {
                "method": "dhf.card.id.get",
                "content": { "gzh_appid": $scope.wxappid, "partner_id": partner_id }
            };

            console.error("下载卡券数据---->", data);
            // return false;

            dataService.cardidGet(data).then(function(result) {
                console.error("下载卡券数据", result, result.errmsg);

                helper.myAlert(result.errmsg, null, null, function() {
                    $scope.getList(1);
                });
            });
        }

        //卡券领取记录
        $scope.logAction = function() {
            $scope.pageSize = 10;

            var card_id = $location.search().card_id;
            var wxappid = $location.search().wxappid;

            $scope.getLogList(card_id, wxappid, 1);
        }

        $scope.getLogList = function(card_id, wxappid, pageNo) {
            var data = {
                "method": "dhf.card.log.get",
                "content": { "card_id": card_id, "page_size": $scope.pageSize, "page_no": pageNo }
            };

            console.error("data-->", data);
            // return false;
            dataService.cardLogGet(data).then(function(result) {
                console.error("卡券领取列表--->", result, result.errmsg);

                if (result.total_count) {
                    $scope.card_id = card_id;
                    $scope.wxappid = wxappid;
                    $scope.card_log = result.card_log;
                    $scope.total_count = result.total_count;
                } else {
                    helper.myAlert("error: 没有用户领取过该优惠券！");
                }
            });
        }

        $scope.hexiaoCoupon = function(code, wxappid) {
            var data1 = {
                "req_method": "POST",
                "authorizer_appid": wxappid,
                "url_prefix": "https://api.weixin.qq.com/card/code/consume?access_token=TOKEN",
                "content": {
                    "code": code
                }
            }

            console.error("核销卡券参数--->", data1);
            // return false;

            dataService.card(data1).then(function(result) {
                console.error("核销 --->", result, result.errmsg);

                if (result.errmsg == "ok") {
                    var card_id = result.card.card_id;
                    var open_id = result.open_id;

                    var data = {
                        "method": "dhf.card.log.update",
                        "content": { "card_id": card_id, "code": code, "open_id": openid }
                    };

                    console.error("data-->", data);
                    // return false;
                    dataService.cardLogUpdate(data).then(function(result) {
                        console.error("卡券领取状态更改--->", result, result.errmsg);
                        if (!result.errcode) {
                            helper.myAlert("success", null, null, function() {
                                $scope.getLogList(card_id, wxappid, 1);
                            });
                        } else {
                            helper.myAlert("error: " + result.errmsg);
                        }
                    });
                } else {
                    helper.myAlert("error: " + result.errmsg);
                }
            });
        }
    }
})();